package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageBean;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.entity.SysUser;
import com.itheima.ydd.service.ISysRoleMenuService;
import com.itheima.ydd.service.ISysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_role")
@Slf4j
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysRoleMenuService sysRoleMenuService;

    @GetMapping("/allData")
    public ResultData allDate(){
        return ResultData.ok("",sysRoleService.list());
    }

    /**
     * 角色管理分页查询
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @GetMapping("/list")

    public ResultData list(Long page, Long limit, String name) {
        log.info("角色管理分页查询");
        //创建分页查询器
        Page<SysRole> pageInfo = new Page<>(page,limit);
        //构建条件构造器
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), SysRole::getName,name);
        sysRoleService.page(pageInfo,queryWrapper);
        PageBean<SysRole> pageBean = new PageBean<>();
        pageBean.setData(pageInfo.getRecords());//封装的具体集合数据
        pageBean.setPageSize(pageInfo.getSize());//每页条数
        pageBean.setCount(pageInfo.getTotal());//总条数
        pageBean.setTotalPages(pageInfo.getPages());//总页数
        pageBean.setCurrentPage(pageInfo.getCurrent());//当前页
        return ResultData.ok("",pageBean);
    }

    /**
     * 角色修改
     *
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getById(String id) {
        SysRoleDto sysRole = sysRoleService.getByCondition(id);
        return ResultData.ok("", sysRole);
    }


    /**
     * 角色添加
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody SysRoleDto sysRoleDto) {
        sysRoleService.add(sysRoleDto);
        return ResultData.ok("角色添加成功", sysRoleDto);
    }

    /**
     * 修改角色
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody SysRoleDto sysRoleDto) {
        log.info("修改角色");
        sysRoleService.updateById(sysRoleDto);
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getRoleId, sysRoleDto.getId());
        sysRoleMenuService.remove(queryWrapper);
        List<String> menuids = sysRoleDto.getMenuids();
        for (String menuid : menuids) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menuid);
            sysRoleMenu.setRoleId(sysRoleDto.getId());
            sysRoleMenuService.save(sysRoleMenu);
        }
        return ResultData.ok("修改成功", "");
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(String id){
        LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRole::getId,id);
        queryWrapper.eq(SysRole::getDefaultData,1);
        SysRole sysRole = sysRoleService.getOne(queryWrapper);
        if (sysRole != null){
            return ResultData.error("系统默认配置,无法删除");
        }
        sysRoleService.removeById(id);
        return ResultData.ok("删除成功",true);
    }
}
